﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using phones.Model;
using System.Collections.ObjectModel;

namespace phones.Repository
{
    public class OrganisationsRepository : BaseRepository<Organisation>
    {
     
        public OrganisationsRepository(phonesEntities context) : base(context) { }
        public OrganisationsRepository() : base() { }

        public ObservableCollection<Organisation> GetList(ServiceNode node)
        {
            if (node == null) return null;
            else
            {

                ObservableCollection<Organisation> list = new ObservableCollection<Organisation>();
                var query = from org in Context.Organisations
                            join dep in Context.Departments on org.ID equals dep.Organisation.ID
                            join c in Context.Contacts on dep.ID equals c.Department.ID
                            where c.City.ServiceNode.ID == node.ID
                            orderby org.Name ascending
                            select org;
                list = new ObservableCollection<Organisation>(query.Distinct().ToList());
                return list;
            }


        }
    }
}
